/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.core.domain;

import javax.persistence.*;
import java.util.Date;
import java.util.UUID;

/**
 * Represents a {@link foo.bar.blog.core.domain.Blog}'s Tag.
 *
 * @author tmjee
 * @version $Date: 2009-03-14 06:53:07 +0000 (Sat, 14 Mar 2009) $ $Id$
 */
@Entity
@EntityListeners({EntityAuditListener.class})
@Table(name="TBL_TAG")
@NamedQueries({
    @NamedQuery(name="tag_findAllTagsByBlog",
        query = "SELECT tag FROM Tag AS tag WHERE tag.blog = :blog"),
    @NamedQuery(name="tag_countAllTagsByBlog",
        query="SELECT count(tag) FROM Tag AS tag WHERE tag.blog = :blog"),
    @NamedQuery(name="tag_findTagById",
        query="SELECT tag FROM Tag as tag WHERE tag.id = :id"),
    @NamedQuery(name="tag_findEntryWithTag",
        query="SELECT entry FROM Entry AS entry WHERE entry.tag = :tag "),
    @NamedQuery(name="tag_countTagByNameAndBlog",
        query="SELECT count(tag) FROM Tag AS tag WHERE tag.name = :name AND tag.blog = :blog")
})
public class Tag implements CreationInfoPersistable, ModificationInfoPersistable, Identifiable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "tagIdGenerator")
    @TableGenerator(name="tagIdGenerator", table = "TBL_ID_GENERATOR",
                    pkColumnName = "ID", valueColumnName = "ID_VALUE",
                    pkColumnValue = "TBL_TAG_CURRENT_ID", initialValue = 1,
                    allocationSize = 10)
    @Column(name="ID", nullable = false)
    private long id;

    @Basic
    @Column(name="NAME")
    private String name;

    @Basic
    @Column(name="DESCRIPTION")
    private String description;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(table = "TBL_TAG", name = "MODIFIER_ID", referencedColumnName = "ID")
    private User modifier;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(table = "TBL_TAG", name = "CREATOR_ID", referencedColumnName = "ID")
    private User creator;

    @Basic
    @Column(name="CREATION_DATE")
    private Date creationDate;

    @Basic
    @Column(name="MODIFICATION_DATE")
    private Date modificationDate;


    @ManyToOne(targetEntity = Blog.class)
    @JoinColumn(table="TBL_TAG", name="BLOG_ID", referencedColumnName = "ID")
    private Blog blog;

    @Basic
    @Column(name="UID")
    private String uid = UUID.randomUUID().toString();


    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public User getModifier() {
        return modifier;
    }

    public void setModifier(User modifier) {
        this.modifier = modifier;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Tag tag = (Tag) o;

        if (uid != null ? !uid.equals(tag.uid) : tag.uid != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (int) (id ^ (id >>> 32));
        result = 31 * result + (uid != null ? uid.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (modifier != null ? modifier.hashCode() : 0);
        result = 31 * result + (creator != null ? creator.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (modificationDate != null ? modificationDate.hashCode() : 0);
        return result;
    }
}
